import * as echarts from "echarts";

// 获取当月最后一天
export const getLastDayOfCurrentMonth = () => {
  var currentDate = new Date();
  var year = currentDate.getFullYear();
  var month = currentDate.getMonth() + 1;
  var lastDay = new Date(year, month, 0).getDate();
  return lastDay;
};

// 获取指定月份最后一天，如：传参2024-09，返回2024-09-30
export const getLastDayOfMonth = yearMonth => {
  const date = new Date(yearMonth);
  date.setMonth(date.getMonth() + 1, 0); // 设置下个月的第一天，然后减去一天得到当前月的最后一天
  return `${yearMonth}-${String(date.getDate()).padStart(2, "0")}`;
};

// 折线图
export const drawLineChart = (dom, xData, yData) => {
  if (dom) {
    let option = {
      // toolbox: {
      //   show: true,
      //   feature: {
      //     saveAsImage: {
      //       title: "点击保存为图片"
      //     }
      //   }
      // },
      grid: {
        left: "15%",
        bottom: "10%"
      },
      tooltip: {
        trigger: "axis"
      },
      xAxis: {
        type: "category",
        data: xData
      },
      yAxis: {
        type: "value",
        name: "万元",
        axisLabel: {
          formatter: function (value) {
            if (value >= 1e12) {
              return (value / 1e12).toFixed(1) + "T"; // 万亿
            } else if (value >= 1e9) {
              return (value / 1e9).toFixed(1) + "B"; // 十亿
            } else if (value >= 1e6) {
              return (value / 1e6).toFixed(1) + "M"; // 百万
            } else if (value >= 1e3) {
              return (value / 1e3).toFixed(1) + "K"; // 千
            }
            return value;
          }
        }
      },
      series: [
        {
          data: yData,
          type: "line"
        }
      ]
    };
    let myChart = echarts.init(dom);
    option && myChart.setOption(option);
    return myChart;
  }
};

// 堆叠折线图
export const drawStackingLineChart = (dom, xData, yData) => {
  if (dom) {
    let seriesData = [];
    yData?.forEach(item => {
      seriesData.push({
        name: item.name,
        type: "line",
        data: item.value
      });
    });

    let option = {
      // toolbox: {
      //   show: true,
      //   feature: {
      //     saveAsImage: {
      //       title: "点击保存为图片"
      //     }
      //   }
      // },
      tooltip: {
        trigger: "axis",
        formatter: function (params) {
          let total = 0;
          total = params.reduce((sum, item) => {
            item.value = item.value ? item.value : 0;
            return sum + Number(item.value);
          }, 0);
          total = total.toFixed(2);
          // 生成内容
          let html = `<div>${params[0].name}</div>`;
          params.forEach(item => {
            html += `<div style="display: flex;align-items: center"><div style="width: 10px;height: 10px;background-color: ${item.color};border-radius: 50%"></div>&nbsp;<div style="flex: 1">${item.seriesName}</div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div style="font-weight: bold;">${item.value ? item.value : "-"}</div></div>`;
          });
          html += `<div style="display: flex"><span style="flex-grow: 1; margin-left: 14px">合计</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold;float: right">${total}</span></d>`;
          return html;
        }
      },
      legend: {
        type: "scroll"
      },
      grid: {
        left: "15%",
        bottom: "10%"
      },
      xAxis: {
        type: "category",
        boundaryGap: false,
        data: xData
      },
      yAxis: {
        type: "value",
        name: "万元",
        axisLabel: {
          formatter: function (value) {
            if (value >= 1e12) {
              return (value / 1e12).toFixed(1) + "T"; // 万亿
            } else if (value >= 1e9) {
              return (value / 1e9).toFixed(1) + "B"; // 十亿
            } else if (value >= 1e6) {
              return (value / 1e6).toFixed(1) + "M"; // 百万
            } else if (value >= 1e3) {
              return (value / 1e3).toFixed(1) + "K"; // 千
            }
          }
        }
      },
      series: seriesData
    };
    let myChart = echarts.init(dom);
    option && myChart.setOption(option);
    return myChart;
  }
};

// 饼图
export const drawPieChart = (dom, data) => {
  if (dom) {
    let option = {
      // toolbox: {
      //   show: true,
      //   top: 20,
      //   feature: {
      //     saveAsImage: {
      //       title: "点击保存为图片"
      //     }
      //   }
      // },
      grid: {
        left: "15%",
        bottom: "10%"
      },
      tooltip: {
        trigger: "item",
        formatter: "{a} <br/>{b}: {c}万元 ({d}%)" // 添加单位"万元"
      },
      legend: {
        show: true,
        type: "scroll",
        right: 20
      },
      series: [
        {
          name: "费用占比",
          type: "pie",
          radius: "50%",
          label: {
            show: true,
            formatter: "{d}%：{b}", // 显示名称和百分比
            position: "outside"
          },
          data,
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: "rgba(0, 0, 0, 0.5)"
            }
          }
        }
      ]
    };
    let myChart = echarts.init(dom);
    option && myChart.setOption(option);
    return myChart;
  }
};

// 柱状图
export const drawBarChart = (dom, xData, yData, args = 0) => {
  let myChart = echarts.init(dom);
  let colors = [
    "#3B82F6",
    "#39C670",
    "#F15645",
    "#40D3FF",
    "#2D52DA",
    "#FFC53D",
    "#13C2C2",
    "#8573E2",
    "#FFEC58",
    "#5470c6",
    "#91cc75",
    "#fac858",
    "#ee6666"
  ];
  let option = {
    // toolbox: {
    //   show: true,
    //   feature: {
    //     saveAsImage: {
    //       title: "点击保存为图片"
    //     }
    //   }
    // },
    grid: {
      left: "15%",
      bottom: "10%"
    },
    tooltip: {
      trigger: "axis"
    },
    xAxis: {
      type: "category",
      data: xData
    },
    yAxis: {
      type: "value",
      name: "万元",
      axisLabel: {
        formatter: function (value) {
          if (value >= 1e12) {
            return (value / 1e12).toFixed(1) + "T"; // 万亿
          } else if (value >= 1e9) {
            return (value / 1e9).toFixed(1) + "B"; // 十亿
          } else if (value >= 1e6) {
            return (value / 1e6).toFixed(1) + "M"; // 百万
          } else if (value >= 1e3) {
            return (value / 1e3).toFixed(1) + "K"; // 千
          }
          return value;
        }
      }
    },
    series: [
      {
        data: yData,
        type: "bar",
        itemStyle: {
          color: colors[args] // 设置柱子颜色为天蓝色
        }
      }
    ]
  };

  option && myChart.setOption(option);
  return myChart;
};

// 堆叠柱状图
export const drawStackingBarChart = (dom, xData, yData) => {
  if (dom) {
    let seriesData = [];
    yData?.forEach(item => {
      seriesData.push({
        stack: "Total",
        name: item.name,
        type: "bar",
        data: item.value
      });
    });
    let option = {
      // toolbox: {
      //   show: true,
      //   feature: {
      //     saveAsImage: {
      //       title: "点击保存为图片"
      //     }
      //   }
      // },
      legend: {
        type: "scroll"
      },
      grid: {
        left: "15%",
        bottom: "10%"
      },
      tooltip: {
        trigger: "axis"
        // formatter: function (params) {
        //   const total = params.reduce((sum, item) => sum + Number(item.value), 0);
        //   // 生成内容
        //   let html = `<div>${params[0].name}</div>`;
        //   params.forEach(item => {
        //     html += `<div><span style="color:${item.color}">${item.seriesName}</span>: ${item.value}</div>`;
        //   });
        //   html += `<div style="color:red">合计: ${total}</div>`;
        //   return html;
        // }
      },
      xAxis: [
        {
          type: "category",
          data: xData
        }
      ],
      yAxis: [
        {
          type: "value",
          name: "万元",
          axisLabel: {
            formatter: function (value) {
              if (value >= 1e12) {
                return (value / 1e12).toFixed(1) + "T"; // 万亿
              } else if (value >= 1e9) {
                return (value / 1e9).toFixed(1) + "B"; // 十亿
              } else if (value >= 1e6) {
                return (value / 1e6).toFixed(1) + "M"; // 百万
              } else if (value >= 1e3) {
                return (value / 1e3).toFixed(1) + "K"; // 千
              }
              return value;
            }
          }
        }
      ],
      series: seriesData
    };
    let myChart = echarts.init(dom);
    option && myChart.setOption(option);
    return myChart;
  }
};
